package com.lingnan.tijian.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lingnan.tijian.domain.CiDetailedReport;
import com.lingnan.tijian.domain.CiReport;
import com.lingnan.tijian.domain.OverallResult;
import com.lingnan.tijian.domain.vo.OverallResultVO;
import com.lingnan.tijian.domain.vo.ReportDetailVO;
import com.lingnan.tijian.mapper.OverallResultMapper;
import com.lingnan.tijian.service.ICiDetailedReportService;
import com.lingnan.tijian.service.ICiReportService;
import com.lingnan.tijian.service.IOverallResultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 无Service业务层处理
 * 
 * @author manba
 * @date 2024-12-26
 */
@Service
public class OverallResultServiceImpl extends ServiceImpl<OverallResultMapper,OverallResult> implements IOverallResultService {
    @Autowired
    private OverallResultMapper overallResultMapper;

    @Autowired
    private ICiDetailedReportService ciDetailedReportService;

    @Autowired
    private ICiReportService ciReportService;


    @Override
    public List<OverallResultVO> selectOverallResultlistByOrderId(Long id) {
        List<OverallResult> orList = lambdaQuery()
                .eq(OverallResult::getOrderId, id)
                .list();
        List<OverallResultVO> orVOList = BeanUtil.copyToList(orList, OverallResultVO.class);
        return orVOList;
    }

    @Override
    public List<ReportDetailVO> selectReportDetailList(Long id) {
        //1.查询订单id对应的检查大类
        List<CiReport> ciReportList = ciReportService.lambdaQuery()
                .eq(CiReport::getOrderId, id)
                .list();
        //1.提取检查项大类中的id
        List<Long> ciIdList = ciReportList.stream()
                .map(CiReport::getCiId)
                .toList();
        //2.查询大类id对应的检查项明细
        List<CiDetailedReport> list = ciDetailedReportService.lambdaQuery()
                .in(CiDetailedReport::getCiId, ciIdList)
                .eq(CiDetailedReport::getOrderId, id)
                .list();
        List<ReportDetailVO> reportDetailVOList = BeanUtil.copyToList(ciReportList, ReportDetailVO.class);
        //3.根据大类id拷贝检查项明细到检查项每一个大类
        for (ReportDetailVO reportDetailVO : reportDetailVOList) {
            List<CiDetailedReport> ciDetailedReportList = list.stream()
                    .filter(ciDetailedReport -> ciDetailedReport.getCiId().equals(reportDetailVO.getCiId()))
                    .toList();
            reportDetailVO.setCiDetailedReportList(ciDetailedReportList);
        }
        return reportDetailVOList;
    }
}
